﻿using Kanban.Business;
using Kanban.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Kanban.Api.Controllers
{
    public class TarefasController : ApiController
    {
        public List<Tarefa> GetByQuadro(int idQuadro)
        {
            var business = new TarefaBusiness();
            List<Tarefa> tarefas = business.ListarPorIdQuadro(idQuadro);
            return tarefas;
        }

        public Tarefa GetById(int id)
        {
            var business = new TarefaBusiness();
            Tarefa tarefa = business.ObterPorId(id);
            return tarefa;
        }        

        // UPDATE
        public Tarefa Put(Tarefa tarefa)
        {
            var tarefaBusiness = new TarefaBusiness();
            var logTarefaBusiness = new LogTarefaBusiness();
            var usuarioBusiness = new UsuarioBusiness();

            Tarefa tarefaAtual = tarefaBusiness.ObterPorId(tarefa.Id);
            Usuario usuario = usuarioBusiness.ObterPorNome(this.User.Identity.Name);

            logTarefaBusiness.RegistrarAlteracao(tarefaAtual, tarefa, usuario.Id);            

            tarefaBusiness.Atualizar(tarefa);

            return tarefaBusiness.ObterPorId(tarefa.Id);
        }
        
        // INSERT
        public Tarefa Post(Tarefa tarefa)
        {
            var tarefaBusiness = new TarefaBusiness();
            var logTarefaBusiness = new LogTarefaBusiness();
            var usuarioBusiness = new UsuarioBusiness();
            
            // Adiciona a nova tarefa
            tarefaBusiness.Adicionar(tarefa);            

            Tarefa tarefaNova = tarefaBusiness.ObterPorId(tarefa.Id);                       
            Usuario usuario = usuarioBusiness.ObterPorNome(this.User.Identity.Name);

            logTarefaBusiness.RegistrarInclusao(tarefaNova, usuario.Id);

            return tarefaNova;
        }

        public Tarefa Delete(int id)
        {
            var business = new TarefaBusiness();
            var tarefa = business.ObterPorId(id);
            business.Remover(tarefa.Id);
            tarefa.Excluido = true;
            return tarefa;
        }
    }
}
